package chapter16.converter;

import chapter11.Role;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.convert.converter.Converter;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-04-20 下午3:07
 */
public class StringToRoleConverter implements Converter<String, Role> {
    @Override
    public Role convert(String s) {
        //空字符串
        if (StringUtils.isEmpty(s)) {
            return null;
        }
        //不包含指定字符
        if (!s.contains("-")) {
            return null;
        }
        String[] arr = s.split("-");
        //字符串长度不对
        if (arr.length != 3) {
            return null;
        }
        Role role = new Role();
        role.setRoleId(Long.parseLong(arr[0]));
        role.setRoleName(arr[1]);
        role.setNote(arr[2]);
        return role;
    }
}
